/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package microlensing;

import algorithm.AlgorithmErrorException;
import algorithm.ObjectiveFunction;
import algorithm.Phenotype;
import javalens.astronomy.Event;
import javalens.astronomy.LightCurve;
import javalens.util.CalculationException;
import util.Util;

/**
 *
 * @author vermaak
 */
public class BinaryLensFitObjectiveFunction implements ObjectiveFunction {
    private LightCurve curve;

    public BinaryLensFitObjectiveFunction(LightCurve curve) {
        this.curve = curve;
    }

    @Override
    public double calculateFitness(Phenotype phenotype) throws AlgorithmErrorException {
        if (!(phenotype instanceof MicrolensEventPhenotype)) {
            throw new AlgorithmErrorException("Requires MicrolensEventPhenotype");
        }
        Event event = ((MicrolensEventPhenotype)phenotype).getEvent();
        try {
            // calculate magnitude
            LightCurve newCurve = LightCurve.createFromTimeArray(curve.getDays(), event);
            double chisq = Util.chiSquared(curve.getMags(),newCurve.getMags(),curve.getErrors());
            return 1.0/chisq;
        } catch (CalculationException ex) {
            throw new AlgorithmErrorException("COuld not calculate fitness", ex);
        }
        
    }
}
